package com.tuanbiao.ui.plan;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.text.InputType;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.abiao.lib_utils.StringUtils;
import com.abiao.lib_utils.ViewUtils;
import com.hjq.toast.ToastUtils;
import com.hzjq.core.callback.ResultCallback;
import com.hzjq.data.entity.CapDelayEntity;
import com.hzjq.data.entity.UIDRule;
import com.hzjq.data.hzjq.entity.CapRecordEntity;
import com.hzjq.lib.BlastKool;
import com.hzjq.lib.bean.PlanInfo;
import com.hzjq.lib.cap.bean.ScanResult;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.AttachPopupView;
import com.lxj.xpopup.core.BasePopupView;
import com.tuanbiao.R;
import com.tuanbiao.base.mvp.BasePresenterActivity;
import com.tuanbiao.constants.Constants;
import com.tuanbiao.controllers.DetonatorController;
import com.tuanbiao.data.entity.KeepSortItem;
import com.tuanbiao.databinding.ActivityDetonatorRegisterBinding;
import com.tuanbiao.dialog.BaseDialog;
import com.tuanbiao.dialog.BaseDialogFragment;
import com.tuanbiao.dialog.DetonatorSortDialog;
import com.tuanbiao.dialog.InputDialog;
import com.tuanbiao.dialog.InsertDetonatorDialog;
import com.tuanbiao.dialog.MessageDialog;
import com.tuanbiao.dialog.SelectDialog;
import com.tuanbiao.dialog.WaitDialog;
import com.tuanbiao.models.DetonatorModel;
import com.tuanbiao.presenter.DetonatorRegisterPresenter;
import com.tuanbiao.ui.plan.adapter.DetonatorRegisterAdapter;
import com.tuanbiao.utils.ClickUtil;
import com.tuanbiao.utils.LibCollections;
import com.tuanbiao.widget.RegexEditText;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author abiao
 * @since 2022/4/8 17:00
 */
public class DetonatorRegisterActivity extends BasePresenterActivity<DetonatorRegisterPresenter, ActivityDetonatorRegisterBinding>
        implements DetonatorController.RegisterView {

    private long mF1Delay;
    private long mF2Delay;
    private DetonatorRegisterAdapter detonatorRegisterAdapter;
    private WaitDialog.Builder dialog;
    private WaitDialog.Builder backupDialog;
    private List<KeepSortItem> keepSort;
    private KeepSortItem keepSortItem;

    public static Intent createIntent(Context context) {
        return new Intent(context, DetonatorRegisterActivity.class);
    }

    @Override
    protected ActivityDetonatorRegisterBinding getBindingView() {
        return ActivityDetonatorRegisterBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        binding.ivToolbarBack.setOnClickListener(getBackListener());
        binding.ivMore.setOnClickListener(v -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            showRightMenu();
        });
        detonatorRegisterAdapter = new DetonatorRegisterAdapter();
        detonatorRegisterAdapter.setOnItemClickListener((adapter, view, position) -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            detonatorRegisterAdapter.toggleSelect(position);
        });
        detonatorRegisterAdapter.addChildClickViewIds(R.id.iv_edit);
        detonatorRegisterAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            CapRecordEntity entity = (CapRecordEntity) adapter.getItem(position);
            showAdapterMenu(view, entity);
        });
        binding.rvContent.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.rvContent.setAdapter(detonatorRegisterAdapter);
        binding.btnF1.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            showDelaySettingDialog(Constants.ScannerKeyCodes.F1, mF1Delay);
        });
        binding.btnF2.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            showDelaySettingDialog(Constants.ScannerKeyCodes.F2, mF2Delay);
        });
        binding.srlContent.setEnableLoadMore(false);
        binding.srlContent.setOnRefreshListener(refreshLayout -> mPresenter.getDetonatorsByPlanId());
        binding.srlContent.autoRefresh();
    }

    @Override
    public void onBackPressed() {
        WaitDialog.Builder builder = new WaitDialog.Builder(getActivity()).setMessage("正在退出");
        builder.show();
        BlastKool.Companion.loader().read().onExit(new ResultCallback<Boolean>() {
            @Override
            public void onResult(Boolean aBoolean) {
                builder.dismiss();
                if (aBoolean) {
                    killMyself();
                } else {
                    ToastUtils.show("退出失败");
                }
            }
        });

    }

    private void showAdapterMenu(View view, CapRecordEntity entity) {
        AttachPopupView attachPopupView = new XPopup.Builder(getActivity())
                .navigationBarColor(getCompatColor(R.color.gray20))
                .hasShadowBg(false)
                .isClickThrough(true)
                .atView(view)  // 依附于所点击的View，内部会自动判断在上方或者下方显示
                .asAttachList(new String[]{"插入", "修改延时", "删除"},
                        new int[]{},
                        (position, text) -> {
                            if (position == 0) {
                                showInsertDialog(entity);
                            } else if (position == 1) {
                                showEditDelayDialog(entity);
                            } else {
                                showDeleteDialog(entity);
                            }
                        }, R.layout.detonator_edit_list, R.layout.detonator_edit/*, Gravity.LEFT*/);
        attachPopupView.show();
    }

    @Override
    public void showLoading() {
        if (null == dialog)
            dialog = new WaitDialog.Builder(getActivity()).setMessage("正在加载....");
        dialog.show();
    }

    @Override
    public void hideLoading() {
        if (null != dialog && dialog.isShowing()) {
            dialog.dismiss();
        }
    }


    @Override
    protected DetonatorRegisterPresenter createPresenter() {
        return new DetonatorRegisterPresenter(new DetonatorModel(), this);
    }

    @Override
    public void refresh() {
        binding.srlContent.autoRefresh();
        mPresenter.init();
    }

    @Override
    public void showStatistics(PlanInfo planInfo) {
        binding.tvDetonatorCount.setText(getString(R.string.detonator_register_statistics_detonator_count, planInfo.getTotalCount()));
//        binding.tvDetonatorCountDetails.setText(getString(R.string.detonator_register_statistics_detonator_array
//                , planInfo.getMaxRow(), planInfo.getMaxHole()));
        binding.tvDetonatorMinDelay.setText(getString(R.string.detonator_register_statistics_min_delay, planInfo.getMinDelayTime()));
        binding.tvDetonatorMaxDelay.setText(getString(R.string.detonator_register_statistics_max_delay, planInfo.getMaxDelayTime()));
    }

//    @Override
//    public void showStatisticsDetonator(long count, long row, long column) {
//        binding.tvDetonatorCount.setText(getString(R.string.detonator_register_statistics_detonator_count, count));
//        binding.tvDetonatorCountDetails.setText(getString(R.string.detonator_register_statistics_detonator_array, row, column));
//    }
//
//    @Override
//    public void showStatisticsDelay(long minDelay, long maxDelay) {
//        binding.tvDetonatorMinDelay.setText(getString(R.string.detonator_register_statistics_min_delay, minDelay));
//        binding.tvDetonatorMaxDelay.setText(getString(R.string.detonator_register_statistics_max_delay, maxDelay));
//    }

    @Override
    public void showF1Delay(long delay) {
        mF1Delay = delay;
        binding.btnF1.setText(getString(R.string.detonator_register_commit_f1, delay));
    }

    @Override
    public void showF2Delay(long delay) {
        mF2Delay = delay;
        binding.btnF2.setText(getString(R.string.detonator_register_commit_f2, delay));
    }

    @Override
    public void setDetonators(List<CapRecordEntity> entities) {
        binding.srlContent.finishRefresh();
        if (LibCollections.isEmpty(entities)) {
            detonatorRegisterAdapter.setEmptyView(ViewUtils.layoutId2View(R.layout.include_no_data));
        }
        detonatorRegisterAdapter.setNewInstance(entities);
    }

    @Override
    public void addDetonators(List<CapRecordEntity> newCaps) {
        detonatorRegisterAdapter.addData(newCaps);
    }
//
//    @Override
//    public void addDetonators(CapRecordEntity newCaps) {
//        detonatorRegisterAdapter.addData(newCaps);
//    }

    @Override
    public void onScannerTypeChange(boolean isScanner) {
        binding.tvToolbarSubTitle.setText(isScanner ? R.string.detonator_register_sub_title_scan : R.string.detonator_register_sub_title_foot_line);
    }

    @Override
    public void backupLoading() {
        if (null == backupDialog)
            backupDialog = new WaitDialog.Builder(getActivity())
                    .setCancelable(false)
                    .setMessage("正在备份中......");
        backupDialog.show();
    }

    @Override
    public void hideBackupLoading() {
        if (null != backupDialog && backupDialog.isShowing()) backupDialog.dismiss();
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mPresenter.copyCap();
        mPresenter.onDestroy();
    }


    //    @Override
//    public void showAuthDialog(List<CapRecordEntity> entities) {
//        new MessageDialog.Builder(getActivity()).setTitle("提示")
//                .setMessage("当前有" + LibCollections.size(entities) + "发雷管未授权,是否现在去授权？")
//                .setListener(new MessageDialog.OnListener() {
//                    @Override
//                    public void onConfirm(BaseDialog dialog) {
//                        AuthOnlineActivity.start(getActivity());
//                        killMyself();
//                        dialog.dismiss();
//                    }
//
//                    @Override
//                    public void onCancel(BaseDialog dialog) {
//                        dialog.dismiss();
//                        killMyself();
//                    }
//                }).show();
//    }

    @Override
    public void showRepeatDialog(int size, ArrayList<CapRecordEntity> repeatCaps) {
        new MessageDialog.Builder(getActivity())
                .setTitle(R.string.common_note)
                .setMessage("当前有" + size + "发重复雷管")
                .setListener(new MessageDialog.OnListener() {
                    @Override
                    public void onConfirm(BaseDialog dialog) {
                        overCaps(repeatCaps);
                    }
                })
                .show();
    }


    private void overCaps(ArrayList<CapRecordEntity> repeatCaps) {
        BasePopupView popupView = new XPopup.Builder(getActivity())
                .hasShadowBg(false)
                .dismissOnBackPressed(false)
                .dismissOnTouchOutside(false)
                .asLoading("正在处理重复雷管")
                .show();
        mPresenter.overRepeatCaps(repeatCaps, new Runnable() {
            @Override
            public void run() {
                popupView.dismiss();
                mPresenter.getDetonatorsByPlanId();
            }
        });
    }

    @Override
    public void showAddRow(boolean addNewRow) {
        binding.btnAddLine.setBackgroundResource(addNewRow ? R.color.common_button_pressed_color : R.color.primary);
        binding.btnAddLine.setText(addNewRow ? R.string.detonator_register_commit_newline_cancel : R.string.detonator_register_commit_newline);
        binding.btnAddLine.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            mPresenter.toggleAddNewRow();
        });
    }

    @Override
    public void showScanRepeat(CapDelayEntity entity, ScanResult scanResult) {
        detonatorRegisterAdapter.select(scanResult.getCap());
        binding.rvContent.scrollToPosition(detonatorRegisterAdapter.getPosition(scanResult.getCap()));
//        binding.rvContent.scrollToPosition();
    }


    @Override
    public void scrollToPosition(String uid) {
        int position = detonatorRegisterAdapter.getPosition(uid);
        binding.rvContent.scrollToPosition(position);
        detonatorRegisterAdapter.toggleSelect(position);
    }

    @Override
    public int getDetonatorCount() {
        return detonatorRegisterAdapter == null ? 0 : LibCollections.size(detonatorRegisterAdapter.getData());
    }

    @Override
    public void showUidError(long uidErrorCount) {
        new MessageDialog.Builder(getActivity())
                .setTitle(R.string.common_warning)
                .setCancelable(false)
                .setMessage("有" + uidErrorCount + "发雷管编码规则不正确！")
                .setAlone(true)
                .show();
    }

    @Override
    public void onDetonatorChange(CapRecordEntity entity) {
        detonatorRegisterAdapter.notifyItemChanged(entity);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return super.onKeyDown(keyCode, event);
        } else {
            return mPresenter.onKeyDown(keyCode, event);
        }
    }

    @Override
    public void killMyself() {
        setResult(RESULT_OK);
        finish();
    }

    private void showRightMenu() {
        View rootView = LayoutInflater.from(getActivity()).inflate(R.layout.pop_menu_detonator_register, null);
        //我这里给的是固定高度  固定高度需要在外边设置  在布局中设置外边不设置是没有效果的
        final PopupWindow window = new PopupWindow(rootView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        window.setBackgroundDrawable(new BitmapDrawable());
        // 设置弹窗外可点击
        window.setOutsideTouchable(true);
        //获取自身的长宽高
        rootView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        //获取需要在其上方显示的控件的位置信息
        int[] locations = new int[2];
        binding.ivMore.getLocationOnScreen(locations);
        window.showAsDropDown(binding.ivMore);
        window.setFocusable(true);
        window.update();


        AppCompatButton btnOnlineInput = rootView.findViewById(R.id.btn_online_input);
        AppCompatButton btnInsert = rootView.findViewById(R.id.btn_insert);
        AppCompatButton btnEditBatch = rootView.findViewById(R.id.btn_edit_batch);
        AppCompatButton btnEditDelay = rootView.findViewById(R.id.btn_edit_delay);
        AppCompatButton btnSort = rootView.findViewById(R.id.btn_sort);
        AppCompatButton btnPlanClear = rootView.findViewById(R.id.btn_plan_clear);
        AppCompatButton btnDelete = rootView.findViewById(R.id.btn_delete);
        AppCompatButton btnPlanSetting = rootView.findViewById(R.id.btn_plan_setting);
        AppCompatButton btnPlanBackup = rootView.findViewById(R.id.btn_plan_backup);
        AppCompatButton btnInsertRow = rootView.findViewById(R.id.btn_insert_row);

        btnEditBatch.setOnClickListener(v -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            if (getDetonatorCount() <= 0) {
                showMessage("暂无雷管数据");
                return;
            }
            window.dismiss();
            showEditDialog();
        });
        btnInsert.setOnClickListener(v -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            if (LibCollections.size(detonatorRegisterAdapter.getData()) >= mPresenter.getMaxCapCount()) {
                showMessage("已达到组网的最大雷管数据，无法再导入雷管数据");
                return;
            }
            window.dismiss();
            showInsertDialog(!LibCollections.isEmpty(detonatorRegisterAdapter.getSelectedData()) ? detonatorRegisterAdapter.getSelectedData().get(0) : null);
        });
        btnInsertRow.setOnClickListener(v -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            window.dismiss();
            showInsertRowDialog();
        });
        btnOnlineInput.setOnClickListener(v -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            mPresenter.menuReadOnline(LibCollections.size(detonatorRegisterAdapter.getData()));
            window.dismiss();
        });
//
        btnEditDelay.setOnClickListener(v -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            if (getDetonatorCount() <= 0) {
                showMessage("无雷管信息，无需清除");
                return;
            }
            window.dismiss();
        });
        btnSort.setOnClickListener(v -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            if (getDetonatorCount() <= 0) {
                showMessage("无雷管信息，无需排序");
                return;
            }
            window.dismiss();
            showSortDialog();
        });
        btnPlanClear.setOnClickListener(v -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            if (getDetonatorCount() <= 0) {
                showMessage("无雷管信息，无需清除");
                return;
            }
            window.dismiss();
            showClearDialog();
        });
        btnDelete.setOnClickListener(v -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            if (LibCollections.isEmpty(detonatorRegisterAdapter.getSelectedData())) {
                showMessage("未选择雷管");
                return;
            }
            window.dismiss();
            showDeleteDialog();
        });

        btnPlanSetting.setOnClickListener(view -> {
            window.dismiss();
        });
        btnPlanBackup.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            if (getDetonatorCount() <= 0) {
                showMessage("无雷管信息，无需备份");
                return;
            }
            window.dismiss();
            showBackupDialog();
        });
    }

    private void showInsertRowDialog() {
        new InputDialog.Builder(getActivity())
                .setTitle("插入排数")
                .setHint("请输入插入排数")
                .setMaxLength(6)
                .setInputType(InputType.TYPE_CLASS_NUMBER)
                .setAutoDismiss(false)
                .setListener(new InputDialog.OnListener() {
                    @Override
                    public void onConfirm(BaseDialog dialog, String content) {
                        if (TextUtils.isEmpty(content)) {
                            showMessage("请输入插入排数");
                            return;
                        }
                        mPresenter.menuSetRow(content);
                        dialog.dismiss();
                    }

                    @Override
                    public void onCancel(BaseDialog dialog) {
                        dialog.dismiss();
                    }
                }).show();
    }

    private void showBackupDialog() {
        new InputDialog.Builder(getActivity())
                .setTitle("方案备份")
                .setHint("请输入备份方案的名称")
                .setAutoDismiss(false)
                .setListener(new InputDialog.OnListener() {
                    @Override
                    public void onConfirm(BaseDialog dialog, String content) {
                        if (TextUtils.isEmpty(content)) {
                            showMessage("请输入备份方案名称");
                            return;
                        }
                        mPresenter.menuBackUpPlan(content);
                        dialog.dismiss();
                    }

                    @Override
                    public void onCancel(BaseDialog dialog) {
                        dialog.dismiss();
                    }
                }).show();
    }

    private void showDeleteDialog(CapRecordEntity entity) {
        new MessageDialog.Builder(getActivity())
                .setTitle("警告")
                .setMessage("是否删除当前雷管数据")
                .setListener(dialog -> mPresenter.deleteDetonator(entity)).show();
    }

    private void showDeleteDialog() {
        new MessageDialog.Builder(getActivity())
                .setTitle("警告")
                .setMessage("是否删除所选雷管数据")
                .setListener(dialog -> mPresenter.menuDeleteSelectedDetonator(detonatorRegisterAdapter.getSelectedData())).show();
    }

    private void showClearDialog() {
        new MessageDialog.Builder(getActivity())
                .setTitle("警告")
                .setMessage("是否删除所有雷管数据")
                .setListener(dialog -> mPresenter.menuClearDetonator()).show();
    }

    private void showSortDialog() {
        new DetonatorSortDialog.Builder(getActivity())
                .setSortClickListener((sortType, orderType) -> mPresenter.menuSort(detonatorRegisterAdapter.getData(), sortType, orderType)).show();
    }

    private void showEditDelayDialog(CapRecordEntity entity) {
        new InputDialog.Builder(getActivity())
                .setTitle("修改延时")
                .setContent(StringUtils.nullToEmpty(String.valueOf(entity.getDelay())))
                .setInputRegex(RegexEditText.REGEX_NUMBER)
                .setInputType(InputType.TYPE_CLASS_NUMBER)
                .setHint("请输入延时,单位ms")
                .setListener(new InputDialog.OnListener() {
                    @Override
                    public void onConfirm(BaseDialog dialog, String content) {
                        if (!TextUtils.isEmpty(content) && TextUtils.isDigitsOnly(content)) {
                            long delay = Long.parseLong(content);
                            if (delay > mPresenter.getMaxDelay()) {
                                showShort("设置延时不能大于" + mPresenter.getMaxDelay() + "ms");
                                return;
                            }
                            entity.setDelay(delay);
                            mPresenter.updateDetonator(entity);
                            dialog.dismiss();
                        } else {
                            showMessage("请输入整数");
                        }
                    }

                    @Override
                    public void onCancel(BaseDialog dialog) {
                        dialog.dismiss();
                    }
                }).show();
    }

    private void showInsertDialog(CapRecordEntity entity) {
        InsertDetonatorDialog.newInstance(mPresenter.getMaxDelay())
                .setListener(new InsertDetonatorDialog.OnConfirmListener() {
                    @Override
                    public void onConfirm(BaseDialogFragment dialogFragment, String uid, String delay, String row, String column, UIDRule rule) {
                        mPresenter.menuInsertDetonator(entity, uid, delay, row, column,rule);
                    }

                    @Override
                    public void onCancel() {

                    }
                })
                .show(getSupportFragmentManager(), "InsertDetonatorDialog");
    }

    private void showEditDialog() {
        // 自定义对话框
        new BaseDialog.Builder<>(this)
                .setBackgroundDimEnabled(true)
                .setContentView(R.layout.dialog_delay_edit)
                .setAnimStyle(BaseDialog.ANIM_SCALE)
                //.setText(id, "我是预设置的文本")
                .setOnClickListener(R.id.ll_keep_sort, new BaseDialog.OnClickListener<View>() {
                    @Override
                    public void onClick(BaseDialog baseDialog, View view) {
                        AppCompatTextView textView = baseDialog.findViewById(R.id.tv_keep_sort);
                        LinearLayout llRowDelay = baseDialog.findViewById(R.id.ll_row_delay);
                        if (null == keepSort) {
                            keepSort = new ArrayList<>();
                            keepSort.add(new KeepSortItem(true));
                            keepSort.add(new KeepSortItem(false));
                        }
                        // 单选对话框
                        SelectDialog.Builder builder = new SelectDialog.Builder(getActivity())
                                .setTitle("排序方式")
                                .setList(keepSort)
                                // 设置单选模式
                                .setSingleSelect();
                        if (null != keepSortItem) {
                            builder.setSelect(keepSortItem.isKeepSort() ? 0 : 1);
                        }
                        // 设置默认选中
                        builder.setListener((SelectDialog.OnListener<KeepSortItem>) (dialog, data) -> {
                            Set<Map.Entry<Integer, KeepSortItem>> entries = data.entrySet();
                            for (Map.Entry<Integer, KeepSortItem> entry : entries) {
                                keepSortItem = entry.getValue();
                            }
                            if (null != keepSortItem && null != textView && null != llRowDelay) {
                                textView.setText(keepSortItem.itemName());
                                llRowDelay.setVisibility(keepSortItem.isKeepSort() ? View.GONE : View.VISIBLE);
                            }
                        }).show();
                    }
                })
                .setOnClickListener(R.id.btn_cancel, (dialog, button) -> dialog.dismiss())
                .setOnClickListener(R.id.btn_confirm, (dialog, button) -> {
                    String startDelay = getString(dialog.findViewById(R.id.et_start_delay));
                    String rowDelay = getString(dialog.findViewById(R.id.et_row_delay));
                    String columnDelay = getString(dialog.findViewById(R.id.et_column_delay));
                    if (!mPresenter.checkUpdateDelays(keepSortItem, startDelay, rowDelay, columnDelay)) {
                        return;
                    }
                    mPresenter.menuUpdateDelays(keepSortItem.isKeepSort(), startDelay, rowDelay, columnDelay);
                    dialog.dismiss();
                }).show();
        keepSortItem = null;
    }

    private void showDelaySettingDialog(@Constants.ScannerKeyCode final int scannerCode, long delay) {
        String delayStr = delay == 0 ? "" : String.valueOf(delay);
        String title = scannerCode == Constants.ScannerKeyCodes.F1 ? "修改F1延时" : "修改F2延时";
        new InputDialog.Builder(getActivity())
                .setTitle(title)
                .setContent(StringUtils.nullToEmpty(delayStr))
                .setInputRegex(RegexEditText.REGEX_NUMBER)
                .setInputType(InputType.TYPE_CLASS_NUMBER)
                .setHint("请输入延时,单位ms")
                .setListener(new InputDialog.OnListener() {
                    @Override
                    public void onConfirm(BaseDialog dialog, String content) {
                        if (!TextUtils.isEmpty(content) && TextUtils.isDigitsOnly(content)) {
                            long delay = Long.parseLong(content);
                            if (delay > mPresenter.getMaxDelay()) {
                                showShort("设置延时不能大于" + mPresenter.getMaxDelay() + "ms");
                                return;
                            }
                            mPresenter.updateFDelay(scannerCode, content);
                            dialog.dismiss();
                        } else {
                            showMessage("请输入整数");
                        }
                    }

                    @Override
                    public void onCancel(BaseDialog dialog) {
                        dialog.dismiss();
                    }
                }).show();
    }

}